package com.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.base.helper.VosHelper;
import com.base.po.param.VosModelList;
import com.base.po.sarg.SargPojo;
import com.base.po.voip.Subscribers;
import com.base.service.UsersService;
import com.base.utils.GsonUtil;
import com.base.utils.Page;

/**
 * 用户通话消费统计查看服务端
 * 
 * @author f 优化日期：10-16
 *
 */
@Controller
@RequestMapping("/admin")
public class CallCountIndexController {

	@Autowired
	private SubscribersService subscribersService; // 用户Service
	@Autowired
	private UsersService usersService; // 代理商Service

	/**
	 * 用户通话消费统计首页
	 * 
	 * @param session 获取参数
	 * @param sp      搜索内容见SargPojo
	 * @return ModelAndView
	 */
	@RequestMapping("/callCount_index")
	public ModelAndView index(HttpSession session, SargPojo sp) {
		Object obj_id = session.getAttribute("adminUserId"); // 登录的用户ID
		Object obj_role = session.getAttribute("adminUsergrade"); // 登录的用户权限
		if (obj_id == null || obj_role == null) {
			return new ModelAndView("jsp/loginCheck"); // 未登录
		}
		String userid = (String) obj_id;
		int role = (int) obj_role;
		Map<String, Object> map = new HashMap<>(); // 存储数据对象
		// 设置分页 获取内容
		Page page = new Page(sp.getCurrentPageNo()); // 实例化分页
		List<Subscribers> subList = null; // 用户消费统计内容集合
		Subscribers sub = null; // 用户对象集合
		// 判断登录权限
		switch (role) {
		case 0: // ADMIN用户显示 全部
			userid = null; // 把userid赋值为空 不进行break 之后进行查找商家下的所有内容 便是全部
		case 2: // 商家用户显示 商家下的用户
			sub = subscribersService.countByCallSA(userid, sp.getMobile(), sp.getUsername(), sp.getShopname()); // 赋值用户对象集合
			page.setRecordTotal(sub.getRole()); // 设置总条数
			subList = subscribersService.selectByCallSA(page, userid, sp.getMobile(), sp.getUsername(),
					sp.getShopname());
			break;
		case 1: // 代理商用户显示 本代理商和本代理商下的代理商以及商家下的用户
			sub = subscribersService.countByCallPA(userid, sp.getMobile(), sp.getUsername(), sp.getShopname()); // 赋值用户对象集合
			page.setRecordTotal(sub.getRole()); // 设置总条数
			subList = subscribersService.selectByCallPA(page, userid, sp.getMobile(), sp.getUsername(),
					sp.getShopname());
			break;
		}
		// VOS读取用户剩余余额
		for (Subscribers s : subList) {
			String result;
			try {
				result = VosHelper.GetVosAccount("{accounts:['"
						+ usersService.selectByPrimaryKey(s.getParentid()).getAgentprefix() + s.getMobile() + "']}"); // 获取用户信息
			} catch (NullPointerException e) {
				result = null;
			}
			if (result != null) {
				VosModelList v1 = GsonUtil.getObject(result, VosModelList.class);
				if (v1.retCode == 0)
					s.setAliaccount(v1.infoCustomers[0].getMoney().toString());
				else
					s.setAliaccount("未知");
			} else
				s.setAliaccount("未知");
		}
		map.put("subList", subList); // 统计集合
		map.put("sp", sp); // 搜索参数
		map.put("callf", sub.getShopEnddate()); // 总消费通话金额
		map.put("pages", page); // 分页
		return new ModelAndView("jsp/callCount_index", map);
	}
}
